package com.qinghe.pro.controller.front;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.qinghe.pro.entity.api.Api;
import com.qinghe.pro.entity.api.ApiColumn;
import com.qinghe.pro.service.api.ApiColumnService;
import com.qinghe.pro.service.api.ApiService;
import com.qinghe.pro.shiro.ShiroUser;
import com.qinghe.utils.Page;
import com.qinghe.utils.SecurityUtils;

@Controller
@RequestMapping("/main/api")
public class MainApiController {

	@Autowired
	@Qualifier("apiServiceImpl")
	private ApiService apiService;

	@Autowired
	@Qualifier("apiColumnServiceImpl")
	private ApiColumnService apiColumnService;

	private static final String VIEW = "/main/api_view";
	private static final String LIST = "/main/api_list";

	/**
	 * 查看api接口详情
	 * 
	 * @param id
	 * @param map
	 */
	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public String view(@PathVariable Integer id, Map<String, Object> map) {
		Api api = apiService.get(id);
		
		map.put("api", api);
		return VIEW;
	}

	/**
	 * 显示api分类和列表
	 * 
	 * @param type
	 * @param api
	 * @param page
	 * @param map
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
	public String list(Integer type, Api api, Page page, Map<String, Object> map) {
		if (type != null) {
			api.setColumn(new ApiColumn(type));
		}
		List<Api> apiList = apiService.findByPage(api, page);
		
		map.put("page", page);
		map.put("apiList", apiList);
		
		ShiroUser shiroUser = SecurityUtils.getShiroUser();
        if (shiroUser != null) {
        	map.put("user", shiroUser.getUser());
        }
        
        // 得到api的总数
        map.put("apiCount", apiService.countAll());
		// 得到api的全部分类
        map.put("menuModule", apiColumnService.getTree());

		return LIST;
	}

}
